package com.xuecheng.content.service.impl;

import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseBaseInfoDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CreateCourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 冯佳鑫 又菜又爱学
 * @version 1.0
 * @DATE now()
 */
@Service
@Slf4j
public class CreateCourseBaseServiceimpl implements CreateCourseBaseService {
    @Autowired
    CourseBaseMapper courseBaseMapper;
    @Autowired
    CourseMarketMapper courseMarketMapper;


    @Transactional
    @Override
    public CourseBaseInfoDto updateCourseBaseInfoDto(Long CompanyId,EditCourseBaseInfoDto editCourseBaseInfoDto) {
       //1.校验，本机构的人才能修改本机构的课程
        long id = editCourseBaseInfoDto.getId();
        CourseBase courseBase1 = courseBaseMapper.selectById(id);
        if (courseBase1==null){
            throw new XueChengPlusException("课程不存在");
        }
        Long companyId2 = courseBase1.getCompanyId();
        if (!(companyId2.equals(CompanyId))){
            throw new XueChengPlusException("非本机构人员不得修改");
        }

        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(editCourseBaseInfoDto,courseBase);
        courseBase.setChangeDate(LocalDateTime.now());
        int i = courseBaseMapper.updateById(courseBase);

        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(editCourseBaseInfoDto,courseMarket);
        Integer integer = saveCourseMarket(courseMarket);
        if (i<=0 || integer<0){
            throw new XueChengPlusException("修改信息失败");
        }
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
        return courseBaseInfoDto;
    }
    @Override
    @Transactional
    public CourseBaseInfoDto createcourse(AddCourseDto dto, long companyid) {


        //2.1将前端发来的数据dto组装到课程内容类
        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(dto,courseBase);
        //设置审核状态
        courseBase.setAuditStatus("202002");
        //设置发布状态
        courseBase.setStatus("203001");
        //机构id
        courseBase.setCompanyId(companyid);
        //添加时间
        courseBase.setCreateDate(LocalDateTime.now());
        //插入课程基本信息表
        int insert = courseBaseMapper.insert(courseBase);
        if (insert<=0){
            throw new XueChengPlusException("新增课程失败");
        }
        //2.2将前端发来的数据dto组装到课程营销类
        //新建课程营销类
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(dto,courseMarket);

        courseMarket.setId(courseBase.getId());
        //应该对课程营销类进行校验并且查询是否存在，如果存在则修改，没有就添加
        //对此专门写个方法进行检验以及修改或者添加
        int i = saveCourseMarket(courseMarket);
        //通过判断返回值，看插入或者修改课程营销表是否成功
        if (i<=0){
            throw new XueChengPlusException("课程营销表修改或者添加失败");
        }
        //3返回数据,封装一个方法
     return    returnCourseBaseInfoDto(courseBase.getId());
    }




    @Autowired
    CourseCategoryMapper courseCategoryMapper;
    private  CourseBaseInfoDto returnCourseBaseInfoDto(long courseid ){
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        //根据课程id去查询课程内容类和课程营销类，并组合到CourseBaseInfoDto返回
        CourseBase courseBase = courseBaseMapper.selectById(courseid);
        if (courseBase==null){
            return null;
        }
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        CourseMarket courseMarket = courseMarketMapper.selectById(courseid);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
      //把mt和st的名字设置，而不是原始的code
        //mt:
        String mt = courseCategoryMapper.selectById(courseBaseInfoDto.getMt()).getName();
        courseBaseInfoDto.setMtName(mt);
        //st:
        String st = courseCategoryMapper.selectById(courseBaseInfoDto.getSt()).getName();
        courseBaseInfoDto.setStName(st);
        return courseBaseInfoDto;
    }
    //用于保存课程营销信息
   private Integer saveCourseMarket(CourseMarket courseMarket){
        //1.校验（收费类型：收费/免费）
        //1.1判断courseMarket是否为空
        if (courseMarket==null){
            return null;
        }
        if (courseMarket.getCharge()==null){
            throw new XueChengPlusException("课程类型并未选择：收费/免费");
        }
        //1.2如果是收费，是否已经写了原价和现价，判断code
        if (courseMarket.getCharge().equals("201001")){
            if (courseMarket.getPrice()==null||courseMarket.getPrice().floatValue()<=0){
                throw new XueChengPlusException("价格未写或者小于等于0");
            }
        }
        //2.检查id是否存在，如果存在，就只是修改;如果不存在，就添加进去
        //先根据id查询，不存在，则添加进去
       if ((courseMarketMapper.selectById(courseMarket))==null){
           int insert = courseMarketMapper.insert(courseMarket);
           return insert;
       }
       //存在的话，直接修改

        int update = courseMarketMapper.updateById(courseMarket);
       return update;

    }
}
